package com.cn.web;

import com.cn.entity.FunLog;
import org.ehcache.Cache;
import org.ehcache.PersistentCacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//import javax.cache.Cache;
import javax.cache.CacheManager;
import java.util.Date;
import java.util.UUID;

/**
 * Created by chenyuncong on 2018/7/31.
 */
@RestController
@RequestMapping("cache")
public class CacheWeb {

    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private PersistentCacheManager persistentCacheManager;

    @RequestMapping("getCache")
    public FunLog getCache(String key){
        Cache<String,FunLog> cache =  persistentCacheManager.getCache("testCache",String.class,FunLog.class);
        return cache.get(key);
    }

    @RequestMapping("setCache")
    public String setCache(String key,FunLog funLog){
        Cache<String,FunLog> cache =  persistentCacheManager.getCache("testCache",String.class,FunLog.class);

        if(StringUtils.isEmpty(funLog.getKey())){
            funLog.setKey(UUID.randomUUID().toString());
        }
        if(StringUtils.isEmpty(funLog.getDate())){
            funLog.setDate(new Date());
        }
        if(StringUtils.isEmpty(funLog.getTime())){
            funLog.setTime(System.currentTimeMillis());
        }
        if(StringUtils.isEmpty(funLog.getAge())){
            funLog.setAge(38);
        }

        cache.put(key,funLog);

        return "OK";
    }
}
